package com.serial.comm.core.exception.handler;

import com.serial.comm.core.constants.SystemConstants;
import com.serial.comm.core.exception.base.BaseBusinessException;
import com.serial.comm.core.reqres.response.ResponseData;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description 全局异常处理: 使用 @RestControllerAdvice + @ExceptionHandler 注解方式实现全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    public static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * @description 处理所有不可知的异常
     */
    @ExceptionHandler({Exception.class})
    public ResponseData globalExceptionHandler(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseData.error(SystemConstants.Message.OPERATE_FAILED);
    }

    /**
     * @description 处理所有业务异常
     */
    @ExceptionHandler({BaseBusinessException.class})
    public ResponseData BusinessExceptionHandler(BaseBusinessException e) {
        log.error(">>>>>>" + e.getMessage(), e);
        return ResponseData.error(e.getCode(), e.getMessage());
    }

}